package producer.broker;

import com.api.Exception.MessageRunTimeException;
import com.api.Message;
import com.api.MessageProducer;
import com.api.MessageType;
import com.api.SendCallback;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import producer.service.RabbitBroker;

import java.util.List;

/**
 * $ProducerClient 发送消息的实现类
 * Created with IDEA
 * Author: LuoHengYi
 * Date: 2022/6/18
 * Time: 10:44
 */
@Component
@RequiredArgsConstructor
public class ProducerClient implements MessageProducer {


   private final RabbitBroker rabbitBroker;

    @Override
    public void send(Message message, SendCallback sendCallback) throws MessageRunTimeException {
        Preconditions.checkNotNull(message.getTopic());

        String messageType = message.getMessageType();
        switch (messageType) {
            //迅速消息，消息不惜要保证可靠性
            case MessageType.RAPID:
                rabbitBroker.rapidSend(message);
                break;
            //确认消息：不需要保证消息的可靠性，但会做消息的confirm确认
            case MessageType.CONFIRM:
                rabbitBroker.confirmSend(message);
                break;
            case MessageType.RELIANT:
                rabbitBroker.reliantSend(message);
                break;
            default:

                break;
        }

    }

    @Override
    public void send(Message message) throws MessageRunTimeException {

    }

    @Override
    public void send(List<Message> messages, SendCallback sendCallback) throws MessageRunTimeException {

    }
}
